package com.abe.bathsystem.utils;


import com.abe.libcore.utils.normal.CommonUtils;
import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;

public class ByteUtils {
    //一字节8位解析
    public static int[] getParseIntData(byte byData) {
        boolean[] x = getParseData(byData);
        int[] y = new int[x.length];
        for (int i = 0; i < x.length; i++) {
            y[i] = x[i] ? 1 : 0;
        }
        return y;
    }

    //一字节8位解析
    public static boolean[] getParseData(byte byData) {
        boolean[] x = new boolean[8];
        x[0] = (byData & 0x01) == 0x01;
        x[1] = (byData & 0x02) == 0x02;
        x[2] = (byData & 0x04) == 0x04;
        x[3] = (byData & 0x08) == 0x08;
        x[4] = (byData & 0x10) == 0x10;
        x[5] = (byData & 0x20) == 0x20;
        x[6] = (byData & 0x40) == 0x40;
        x[7] = (byData & 0x80) == 0x80;
        return x;
    }

    //一字节8位解析
    public static byte getInverseParse(boolean[] data) {
        int x = 0;
        for (int i = 0; i < data.length; i++) {
            x = x + (data[i] ? 1 : 0) * (int) Math.pow(2, i);
        }
        return (byte) x;
    }


    //byte转换
    public static int byteToInt(byte data) {
        return data & 0xFF;
    }

    /*将int转为低字节在前，高字节在后的byte数组
    b[0] = 11111111(0xff) & 01100001
    b[1] = 11111111(0xff) & (n >> 8)00000000
    b[2] = 11111111(0xff) & (n >> 8)00000000
    b[3] = 11111111(0xff) & (n >> 8)00000000
    */
    public byte[] IntToByteArray(int n) {
        byte[] b = new byte[4];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        b[2] = (byte) (n >> 16 & 0xff);
        b[3] = (byte) (n >> 24 & 0xff);
        return b;
    }

    //将低字节在前转为int，高字节在后的byte数组(与IntToByteArray1想对应)
    public int ByteArrayToInt(byte[] bArr) {
        if (bArr.length != 4) {
            return -1;
        }
        return (int) ((((bArr[3] & 0xff) << 24)
                | ((bArr[2] & 0xff) << 16)
                | ((bArr[1] & 0xff) << 8)
                | ((bArr[0] & 0xff) << 0)));
    }

    /*将int转为低字节在后，高字节在前的byte数组
   b[0] = 11111111(0xff) & 01100001
   b[1] = 11111111(0xff) & 00000000
   b[2] = 11111111(0xff) & 00000000
   b[3] = 11111111(0xff) & 00000000
   */
    public static byte[] IntToByteArray2(int value) {
        byte[] src = new byte[4];
        src[0] = (byte) ((value >> 24) & 0xFF);
        src[1] = (byte) ((value >> 16) & 0xFF);
        src[2] = (byte) ((value >> 8) & 0xFF);
        src[3] = (byte) (value & 0xFF);
        return src;
    }

    //将高字节在前转为int，低字节在后的byte数组(与IntToByteArray2想对应)
    public static int ByteArrayToInt2(byte[] bArr) {
        if (bArr.length != 4) {
            return -1;
        }
        return (int) ((((bArr[0] & 0xff) << 24)
                | ((bArr[1] & 0xff) << 16)
                | ((bArr[2] & 0xff) << 8)
                | ((bArr[3] & 0xff) << 0)));
    }

    /**
     * 将byte数组转化成String,为了支持中文，转化时用GBK编码方式
     */
    public String ByteArrayToString(byte[] valArr, int maxLen) {
        String result = null;
        int index = 0;
        while (index < valArr.length && index < maxLen) {
            if (valArr[index] == 0) {
                break;
            }
            index++;
        }
        byte[] temp = new byte[index];
        System.arraycopy(valArr, 0, temp, 0, index);
        try {
            result = new String(temp, "GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 将String转化为byte,为了支持中文，转化时用GBK编码方式
     */
    public byte[] StringToByteArray(String str) {
        byte[] temp = null;
        try {
            temp = str.getBytes("GBK");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return temp;
    }
}
